<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title></title>

    <style type="text/css">
        html, body {
            width: 100%;
            height: 100%;
            margin: 0;
            padding: 0;
            overflow: hidden;
        }

        #map {
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>

    <div id="map"></div>
    <canvas id="canvas"></canvas>

    <script type="text/javascript" src="//apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ"></script>
    <script type="text/javascript" src="https://huiyan-fe.github.io/dataset-manager/static/xlsx.full.min.js"></script>
    <script type="text/javascript" src="https://huiyan-fe.github.io/dataset-manager/static/papaparse.min.js"></script>
    <script type="text/javascript" src="https://huiyan-fe.github.io/dataset-manager/dist/DataSetManager.js"></script>
    <script type="text/javascript" src="../../build/mapv.js"></script>

    <script type="text/javascript">

        // 百度地图API功能
        var map = new BMap.Map("map", {
            enableMapClick: false
        });    // 创建Map实例
        map.centerAndZoom(new BMap.Point(105.403119, 38.028658), 5);  // 初始化地图,设置中心点坐标和地图级别
        map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

        map.setMapStyle({
            styleJson: [
                {
                    "featureType": "all",
                    "elementType": "all",
                    "stylers": {
                        "lightness": 40,
                        "saturation": -100
                    }
                },
				{
                    "featureType": "administrative",
                    "elementType": "labels",
                    "stylers": {
                        "visibility": "on"
                    }
                },
				{
                    "featureType": "poi",
                    "elementType": "labels",
                    "stylers": {
                        "visibility": "off"
                    }
                },
                {
                    "featureType": "road",
                    "elementType": "labels",
                    "stylers": {
                        "visibility": "off"
                    }
                }
            ]
        });

        var randomCount = 1000;

        var data = [];

        var citys = ["北京","天津","上海","重庆","石家庄","太原","呼和浩特","哈尔滨","长春","沈阳","济南","南京","合肥","杭州","南昌","福州","郑州","武汉","长沙","广州","南宁","西安","银川","兰州","西宁","乌鲁木齐","成都","贵阳","昆明","拉萨","海口"];

        // 构造数据
        while (randomCount--) {
            var cityCenter1 = mapv.utilCityCenter.getCenterByCityName(citys[parseInt(Math.random() * citys.length)]);
            var cityCenter2 = mapv.utilCityCenter.getCenterByCityName(citys[parseInt(Math.random() * citys.length)]);
            data.push({
                geometry: {
                    type: 'LineString',
                    coordinates: [[cityCenter1.lng - 1 + Math.random() * 1, cityCenter1.lat - 1 + Math.random() * 1], [cityCenter2.lng - 1 + Math.random() * 1, cityCenter2.lat - 1 + Math.random() * 1]]
                },
                count: 30 * Math.random()
            });
        }

        var intensity = new mapv.utilDataRangeIntensity({
            maxSize: 10,
            gradient: {
				0: 'green',
				0.5: 'yellow',
				1: 'red',
			},
            minSize: 1,
            max: 5000
        });

        var legend = intensity.getLegend({
        });
        legend.style.cssText = "position: absolute;bottom: 10px; right: 10px;";
        document.body.appendChild(legend);

        $.get('od.csv', function(csvstr) {
            var dataSet = mapv.csv.getDataSet(csvstr);
            var data = dataSet.get();
            data = data.filter(function (item) {
                var cityCenter1 = mapv.utilCityCenter.getCenterByCityName(item['出发城市']);
                var cityCenter2 = mapv.utilCityCenter.getCenterByCityName(item['目的城市']);
                return cityCenter1 && cityCenter2;
            });
            data = data.map(function (item) {
                var cityCenter1 = mapv.utilCityCenter.getCenterByCityName(item['出发城市']);
                var cityCenter2 = mapv.utilCityCenter.getCenterByCityName(item['目的城市']);
                item.geometry = {
                    type: 'LineString',
                    coordinates: [[cityCenter1.lng, cityCenter1.lat], [cityCenter2.lng, cityCenter2.lat]]
                }
                item.count = item['迁徙量'];
                item.lineWidth = intensity.getSize(item['迁徙量']);
                return item;
            });
            data = data.sort(function (a, b) {
                return a.count - b.count;
            });
            lineDataSet.set(data);
        });

        var lineDataSet = new mapv.DataSet(data);

        var options = {
            strokeStyle: 'rgba(255, 50, 50, 0.3)',
            //globalCompositeOperation: 'lighter',
            //shadowColor: 'rgba(255, 50, 50, 1)',
            //shadowBlur: 20,
            lineWidth: 2,
            draw: 'simple'
        }

        var mapvLayer = new mapv.baiduMapLayer(map, lineDataSet, options);

        var dataSetManager = new DataSetManager({
            onUploaded: function() {
                console.log('onUploaded');
                console.log(dataSetManager.getData());
                var uploadData = dataSetManager.getData();
                var newData = [];
                var maxCount = uploadData[0].count;
                var minCount = uploadData[0].count;
                for (var i = 0; i < uploadData.length; i++) {
                    maxCount = Math.max(uploadData[i].count, maxCount);
                    minCount = Math.min(uploadData[i].count, minCount);
                }
                intensity.setMax(maxCount / 2);
                intensity.setMin(minCount);
                for (var i = 0; i < uploadData.length; i++) {
                    var item = uploadData[i];
                    var start = item['起点区坐标'].split(',');
                    var end = item['终点区坐标'].split(',');
                    newData.push({
                        geometry: {
                            type: 'LineString',
                            coordinates: [[start[0], start[1]], [end[0], end[1]]]
                        },
                        strokeStyle: intensity.getColor(item.count),
                        lineWidth: intensity.getSize(item.count),
                        count: item.count
                    });
                }

                newData = newData.sort(function (a, b) {
                    return parseFloat(a.count) - parseFloat(b.count);
                });
                console.log('newData', newData);
                
                lineDataSet.set(newData);
            }
        });

        dataSetManager.addControl();

        </script>
	
</body>
</html>
